// Copyright 2019 The rhyme_lph Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// I [author] rhyme_lph
// D [email]  rhymelph@gmail.com
// g [github] https://github.com/rhymelph

import 'dart:async';

import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/src/entities/employee_contact_entity.dart';
import 'package:finance_app/src/http_service/http_employee_contact_service.dart';
import 'package:finance_app/src/mixin/search_data_map_mixin.dart';
import 'package:finance_app/src/provider/hr_payroll/leave/leave_adjustment_provider.dart';
import 'package:finance_app/src/router_gen/router_gen_providers.dart';
import 'package:finance_app/src/widgets/bottom_sheet/got_it_info_bottom_sheet.dart';
import 'package:flutter/material.dart';

class EmployeeBatchSelectProvider
    extends BaseProvider<List<EmployeeContactEntity>>
    with
        SearchDataMapMixin<List<EmployeeContactEntity>, EmployeeContactEntity> {
  List<EmployeeContactEntity> currentSelectList = [];
  bool isSelectMode = false;
  bool isPayslip = false;

  @override
  Future get dataFuture => HttpEmployeeContactService.contactList("open");

  void onCancel() {}

  void onDelete() {}

  void onSelect() {
    if (currentSelectList.length == 0) return;
    if (isSelectMode == true) {
      LeaveAdjustmentProvider pro = Providers.get<LeaveAdjustmentProvider>(
          RouterGenProviders.leaveAdjustmentPage, null);
      if (pro != null) {
        pro.onChangeIsAll(currentSelectList.length == data.length);
      }
      obtainContext?.call((context) {
        Navigator.of(context).pop(currentSelectList);
      });
    } else {
      Routes.navigateTo(RouterGenProviders.setSalaryPeriodPage, params: {
        'data': currentSelectList,
      });
    }
  }

  void onAddEmployee() async {
    final result = await Routes.navigateTo(RouterGenProviders.newEmployeePage);
    if (result != null && result is EmployeeContactEntity) {
      searchData.clear();
      data.add(result);
      transformData(data, false);
      if (result.canGeneratePayslip) currentSelectList.add(result);
      update();
    }
  }

  void onSelectDetail(EmployeeContactEntity value) {
    if (isPayslip && !value.canGeneratePayslip) {
      obtainContext?.call((context) {
        showUnableGeneratePayslipBottomSheet(context);
      });
      return;
    }
    if (currentSelectList.contains(value)) {
      currentSelectList.remove(value);
    } else {
      currentSelectList.add(value);
    }
    update();
  }

  @override
  FutureOr<List<EmployeeContactEntity>> transformData(
      List<EmployeeContactEntity> d, bool isCache) {
    d.sort((a, b) => a.name.compareTo(b.name));
    for (final item in d) {
      searchDataClassification(item);
    }
    return d;
  }

  void searchDataClassification(EmployeeContactEntity item) {
    if (item.name.length > 0) {
      String firstName = item.name[0].toUpperCase();
      if (searchData.containsKey(firstName.toUpperCase())) {
        searchData[firstName].add(item);
      } else {
        searchData[firstName] = [item];
      }
    } else {
      if (searchData['*'] != null) {
        searchData["*"].add(item);
      } else {
        searchData['*'] = [item];
      }
    }
  }

  void onDeselectAll() {
    currentSelectList.clear();
    update();
  }

  void onSelectAll() {
    currentSelectList.clear();
    if (isPayslip) {
      currentSelectList
          .addAll(data.where((element) => element.canGeneratePayslip));
    } else {
      currentSelectList.addAll(data);
    }
    update();
  }
}
